package com.b2bpsssm.controller.capital;
  //收款单表

import com.b2bpsssm.common.R;
import com.b2bpsssm.pojo.Receipt;
import com.b2bpsssm.pojo.SalesPassOrder;
import com.b2bpsssm.service.ReceiptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;


@Controller
@Api(value = "收款单数据测试",tags = "收款单API")
public class ReceiptController {

@Autowired
private ReceiptService receiptService;

        // 查询收款单信息 有分页
        @RequestMapping(value = "/receipt/queryReceiptList",method = {RequestMethod.GET},produces = "application/json;charset=utf-8")
        @ApiOperation(value = "收款单信息 ",notes = " 查询收款单信息 有分页",httpMethod = "get",response = R.class)
        @ApiImplicitParams({
                @ApiImplicitParam(name = "page", value = "当前页码数", required = true),
                @ApiImplicitParam(name = "limit", value = "每页条数", required = true),
        })
        @ResponseBody
        public R queryReceiptList(Integer page, Integer limit){
        // 调用Service层进行业务逻辑处理
        return receiptService.queryReceiptList(page,limit);
            }


       //根据单据标号查询收款单详情
       @RequestMapping(value = "/receipt/queryReceiptByNumber", method = {RequestMethod.GET}, produces = "application/json;charset=utf-8")
       @ApiOperation(value = "收款单信息 ",notes = "根据单据标号查询收款单详情",httpMethod = "get",response = R.class)
       @ApiImplicitParams({
               @ApiImplicitParam(name = "document_no", value = "单据编号", required = true),

       })
       @ResponseBody
       public R queryReceiptByNumber(String document_no){
           // 调用Service层进行业务逻辑处理
           return receiptService.queryReceiptByNumber(document_no);

       }
       //根据付款方查询收款单
       @RequestMapping(value = "/receipt/queryReceiptByPayer", method = {RequestMethod.GET}, produces = "application/json;charset=utf-8")
       @ApiOperation(value = "收款单信息 ",notes = "根据付款方查询收款单",httpMethod = "get",response = R.class)
       @ApiImplicitParams({
               @ApiImplicitParam(name = "page", value = "当前页码数", required = true),
               @ApiImplicitParam(name = "limit", value = "每页条数", required = true),
               @ApiImplicitParam(name = "customer", value = "付款方", required = true),
       })
       @ResponseBody
       public R queryReceiptByPayer(Integer page, Integer limit, String customer){

           return receiptService.queryReceiptByPayer(page,limit,customer);

      }
                  //上传附件
//    @RequestMapping(value = "/receipt/accessory",method = RequestMethod.POST)
//    @ResponseBody
//    public R updateReceiptAccessory(@Validated Receipt receipt, HttpServletRequest request) throws IOException {
//        R r = new R();
//        if (receipt.getReceipt_accessory().isEmpty()) {
//            System.out.println("上传失败");
//            r.setCode(500);
//            r.setMassage("上传文件不能为空--上传失败");
//        } else {
//            System.out.println("开始上传");
//            String uploadPath = request.getServletContext().getRealPath("") + File.separator + "upload" + File.separator;
//            File file = new File(uploadPath);
//            if(!file.exists()){
//                file.mkdirs();
//            }
//            MultipartFile multipartFile = receipt.getReceipt_accessory();
//            String fileName = multipartFile.getOriginalFilename();
//            File newFile = new File(uploadPath + fileName);
//            System.out.println("newFile=" + newFile);
//            FileCopyUtils.copy(multipartFile.getBytes(), newFile);
//            r.setCode(200);
//            r.setMassage("上传成功");
//        }
//        return r;
//    }


                      //添加付款单
    @RequestMapping(value = "/receipt/addReceipt", method = {RequestMethod.POST}, produces = "application/json;charset=utf-8")
    @ApiOperation(value = "收款单信息 ",notes = "添加付款单",httpMethod = "POST",response = R.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "document_no", value = "单据编号", required = true),
            @ApiImplicitParam(name = "customer", value = "付款方", required = true),
            @ApiImplicitParam(name = "operator", value = "经手人", required = true),
            @ApiImplicitParam(name = "sub_date", value = "创建时间", required = true),
            @ApiImplicitParam(name = "discount_amount", value = "折扣金额", required = true),
            @ApiImplicitParam(name = "clear_from", value = "结算方式", required = true),
            @ApiImplicitParam(name = "total_amount", value = "总计金额", required = true),
            @ApiImplicitParam(name = "paid_amount", value = "实收金额", required = true),
            @ApiImplicitParam(name = "note", value = "备注", required = false),
    })
    @ResponseBody
    public R addReceipt(@RequestBody SalesPassOrder salesPassOrder){

        return receiptService.addReceipt(salesPassOrder);
    }

    //    以付款方关键字 时间范围 进行关键字查询
    @RequestMapping(value = "/receipt/selReceiptLikeReceiptPayer",method = RequestMethod.GET)
    @ApiOperation(value = "收款单信息 ",notes = " 以付款方关键字 时间范围 进行关键字查询",httpMethod = "get",response = R.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码数", required = true),
            @ApiImplicitParam(name = "limit", value = "每页条数", required = true),
            @ApiImplicitParam(name = "customer", value = "付款方", required = true),
            @ApiImplicitParam(name = "sub_date1", value = "开始时间", required = true),
            @ApiImplicitParam(name = "sub_date2", value = "结束时间", required = true),
    })
    @ResponseBody
    public R selReceiptLikeReceiptPayer(Integer page, Integer limit, String sub_date1, String sub_date2, String customer) {
      return   receiptService.selReceiptLikeReceiptPayer(page,limit,sub_date1,sub_date2,customer);

    }

    //前端点击新增收款单据编号，为前端返回一个单据编号
    @RequestMapping(value = "/receipt/addReceiptNumber")
    @ResponseBody
    public R addReceiptNumber() {
        //创建结果集对象
        R result = new R();
        //销售单的前置固定标识
        String str = "FK";
        //生成时间戳
        long time = System.currentTimeMillis();
        //将时间戳转换为字符串
        String s = String.valueOf(time);
        //拼接字符串生成订单编号
        String receipt_number = str + s;
        result.setCode(200);
        result.setMassage("单据编号生成成功");
        result.setData(receipt_number);
        return result;
    }

}








